package com.yanqu.road.dao.impl.activity.chinesechess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessData;
import com.yanqu.road.entity.activity.chinesechess.ChineseChessPracticeUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ChineseChessPracticeUserDataDaoImpl extends TempDataObjectDao<ChineseChessPracticeUserData> {

    public static String getUserDataSql = "select * from t_u_activity_chinese_chess_practice where activityId=? and userId=?";
    public ChineseChessPracticeUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
                call.setLong(i++,userId);
                rs = call.executeQuery();
                while (rs.next()) {
                    ChineseChessPracticeUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDataSql = "insert into t_u_activity_chinese_chess_practice(" +
            "activityId,userId,score,step,chessCount,nextChessData,chessData,updateTime,gameNo,nextChessTurn," +
            "gameStatus,isReviveSelf,reviveSelfTimes,ingotReviveTimes,powerTimes,shuaiTimes,passTime) " +
            " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ChineseChessPracticeUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStep()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChessCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getNextChessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getChessMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNextChessTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameStatus()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReviveSelf()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveSelfTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getIngotReviveTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPowerTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getShuaiTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassTime()));

        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_u_activity_chinese_chess_practice set " +
            " score=?,step=?,chessCount=?,nextChessData=?,chessData=?,updateTime=?,gameNo=?,nextChessTurn=?," +
            " gameStatus=?,isReviveSelf=?,reviveSelfTimes=?,ingotReviveTimes=?,powerTimes=?,shuaiTimes=?,passTime=?" +
            " where activityId=? and userId=?";
    @Override
    protected boolean updateData(ChineseChessPracticeUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getScore().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStep()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChessCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getNextChessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getChessMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNextChessTurn()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getGameStatus()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReviveSelf()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveSelfTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getIngotReviveTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPowerTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getShuaiTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassTime()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ChineseChessPracticeUserData parseData(ResultSet rs) throws SQLException {
        ChineseChessPracticeUserData data = new ChineseChessPracticeUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setScore(new BigInteger(rs.getString("score")));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setStep(rs.getInt("step"));
        data.setChessCount(rs.getInt("chessCount"));
        data.setGameNo(rs.getInt("gameNo"));
        data.setNextChessTurn(rs.getInt("nextChessTurn"));

        data.setGameStatus(rs.getInt("gameStatus"));
        data.setReviveSelf(rs.getBoolean("isReviveSelf"));
        data.setReviveSelfTimes(rs.getInt("reviveSelfTimes"));
        data.setIngotReviveTimes(rs.getInt("ingotReviveTimes"));
        data.setPowerTimes(rs.getInt("powerTimes"));
        data.setShuaiTimes(rs.getInt("shuaiTimes"));
        data.setPassTime(rs.getInt("passTime"));


        String nextChessStr  = rs.getString("nextChessData");
        if(!StringUtils.isNullOrEmpty(nextChessStr)){
            Map<Integer, ChineseChessData> result = JSON.parseObject(nextChessStr, new TypeReference<Map<Integer, ChineseChessData>>() {});
            if(result != null){
                data.setNextChessMap(result);
            }
        }
        String chessStr  = rs.getString("chessData");
        if(!StringUtils.isNullOrEmpty(chessStr)){
            Map<Integer, ChineseChessData> result = JSON.parseObject(chessStr, new TypeReference<Map<Integer, ChineseChessData>>() {});
            if(result != null){
                data.setChessMap(result);
            }
        }
        return data;
    }

}
